package com.hifar.demo3;

import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import com.hifar.demo3.model.DmData;
import com.hifar.demo3.model.DmTable;
import com.hifar.demo3.model.ProcedureStep;
import com.hifar.demo3.pojo.TableCell;
import com.hifar.demo3.pojo.TableRow;

public class CompleteIntegrationExample {
    public static void main(String[] args) {
        try {
            // 1. 初始化解析器
            S1000DParser dmParser = new S1000DParser();
            OperationDataParser opParser = new OperationDataParser();
            
            // 2. 解析操作数据文件
            try (InputStream opStream = new FileInputStream("src/main/resources/data/DATA-DXJG0001A.xml")) {
                opParser.parse(opStream);
            }
            
            // 3. 设置操作数据解析器
            dmParser.setOperationDataParser(opParser);
            
            // 4. 解析DM文件并填充操作数据
            DmData dmData;
            try (InputStream dmStream = new FileInputStream("src/main/resources/data/DXJG0001A.xml")) {
                dmData = dmParser.parse(dmStream);
            }
            
            // 5. 处理特定数据类型的值
            processTypedValues(dmData);
            
            // 6. 输出结果
            printResults(dmData);
            
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    private static void processTypedValues(DmData dmData) {
        // 处理表格中的数值型数据
        for (DmTable table : dmData.getTables()) {
            for (TableRow row : table.getRows()) {
                for (TableCell cell : row.getCells()) {
                    if (cell.getContent() != null && cell.getContent().matches("-?\\d+(\\.\\d+)?")) {
                        cell.setNumericValue(Double.parseDouble(cell.getContent()));
                    }
                }
            }
        }
        
        // 处理步骤中的操作数据
        for (ProcedureStep step : dmData.getMainProcedureSteps()) {
            Map<String, String> typedData = new HashMap<>();
            for (Map.Entry<String, String> entry : step.getOperationData().entrySet()) {
                // 根据dataID判断类型
                if (entry.getKey().contains("numeric")) {
                    typedData.put(entry.getKey(), 
                        OperationDataProcessor.parseValue(entry.getValue(), "numeric").toString());
                } else if (entry.getKey().contains("checkbox")) {
                    typedData.put(entry.getKey(), 
                        OperationDataProcessor.parseValue(entry.getValue(), "checkbox").toString());
                } else {
                    typedData.put(entry.getKey(), entry.getValue());
                }
            }
            step.setOperationData(typedData);
        }
    }
    
    private static void printResults(DmData dmData) {
        System.out.println("=== 检查报告 ===");
        System.out.println("项目: " + dmData.getDmIdent().getTechName());
        System.out.println("版本: " + dmData.getDmIdent().getIssueNumber());
        System.out.println("日期: " + dmData.getDmIdent().getIssueDate());
        
        System.out.println("\n=== 检查结果 ===");
        for (ProcedureStep step : dmData.getMainProcedureSteps()) {
            if ("检验结论".equals(step.getTitle())) {
                String conclusion = step.getOperationData().values().iterator().next();
                System.out.println("结论: " + conclusion);
                break;
            }
        }
        
        System.out.println("\n=== 详细数据 ===");
        for (DmTable table : dmData.getTables()) {
            System.out.println("\n表格: " + table.getTitle());
            for (TableRow row : table.getRows()) {
                for (TableCell cell : row.getCells()) {
                    System.out.print((cell.getContent() != null ? cell.getContent() : "") + "\t");
                }
                System.out.println();
            }
        }
    }
}